﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;

namespace Yule8xWechat.Applib
{
    public class LoginPageBase
    {
        public static WxUserInfoResult GetLoginInfo(bool checkDatabase=true)
        {
            WxUserInfoResult wxUser=null;
            // 判断登录
            object loginData = HttpContext.Current.Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                HttpContext.Current.Session["login"] = null;
                return null;
            }
            if (wxUser.user_id < 1)
            {
                HttpContext.Current.Session["login"] = null;
                return null;
            }
            if (checkDatabase)
            {
                string sqlstmt = string.Format("select count(0) from t_user where user_id=" + wxUser.user_id);
                int count = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteCount(sqlstmt);
                if (count < 1)
                {
                    HttpContext.Current.Session["login"] = null;
                    return null;
                }
            }
            return wxUser;
        }
    }
}